package com.situ.skymallproductreviewapi.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.model.ProductReview;
import com.situ.service.ProductReviewService;
import com.situ.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "api/v1/productReviews")
public class ProductReviewApi {
    private ProductReviewService productReviewService;
     @Autowired
    public void setProductReviewService(ProductReviewService productReviewService) {
        this.productReviewService = productReviewService;
    }

    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize, ProductReview productReview) {
        Page<ProductReview> page = new Page<>(pageNo, pageSize);
        page = this.productReviewService.findAll(page, productReview);
        return ResponseEntity.ok(JsonResult.success(page));
    }
    @PostMapping
    public ResponseEntity<JsonResult<?>> save(@RequestBody ProductReview productReview) {
        boolean success = this.productReviewService.save(productReview);
        if (success) {
            return ResponseEntity.ok(JsonResult.success());
        } else {
            return ResponseEntity.ok(JsonResult.fail("保存评价失败"));
        }
    }

    @PutMapping
    public ResponseEntity<JsonResult<?>> update(@RequestBody ProductReview productReview) {
        boolean success = this.productReviewService.update(productReview);
        if (success) {
            return ResponseEntity.ok(JsonResult.success());
        } else {
            return ResponseEntity.ok(JsonResult.fail("修改评价失败"));
        }
    }

    @DeleteMapping
    public ResponseEntity<JsonResult<?>> deleteByIds(@RequestBody Integer[] ids) {
        int count = this.productReviewService.deleteByIds(List.of(ids));
        if (count == 0) {
            return ResponseEntity.ok(JsonResult.fail("删除评价失败"));
        } else {
            return ResponseEntity.ok(JsonResult.success(count));
        }
    }
       @GetMapping("/state/{productId}")
      public ResponseEntity<JsonResult<?>>  stateByProductId(@PathVariable Integer productId){
          return ResponseEntity.ok(JsonResult.success(this.productReviewService.stateByProductId(productId)));
      }
}
